package com.lsb.blog.controller;


import com.lsb.blog.common.aop.LogAnnotation;
import com.lsb.blog.common.cache.Cache;
import com.lsb.blog.response.Result;
import com.lsb.blog.service.ArticleService;
import com.lsb.blog.vo.Archives;
import com.lsb.blog.vo.ArticleVo;
import com.lsb.blog.vo.RedisArticleVo;
import com.lsb.blog.vo.params.ArticleParam;
import com.lsb.blog.vo.params.PageParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liusibo
 * @since 2021-08-01
 */
@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //分页查询首页/对应类别页 文章列表
    @PostMapping("/articles")
    @LogAnnotation(module = "文章",operation = "分页查询文章列表")
    public Result articles(@RequestBody PageParams pageParams) {
        //ArticleVo 页面接收的数据
        List<ArticleVo> articles = articleService.listArticlesPage(pageParams);
        return Result.ok().data(articles);
    }

    //首页 最热文章
    @PostMapping("/articles/hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle(){
        int limit = 2;
        List<ArticleVo> articleVoList = articleService.hotArticle(limit);
        List<RedisArticleVo> redisArticleVoList = copyList(articleVoList);
        return Result.ok().data(redisArticleVoList);
    }
    private List<RedisArticleVo> copyList(List<ArticleVo> articleVoList){
        List<RedisArticleVo> redisArticleVoList = new ArrayList<>();
        for (ArticleVo articleVo : articleVoList) {
            redisArticleVoList.add(copy(articleVo));
        }
        return redisArticleVoList;
    }
    private RedisArticleVo copy(ArticleVo articleVo){
        RedisArticleVo redisArticleVo = new RedisArticleVo();
        BeanUtils.copyProperties(articleVo,redisArticleVo);
        redisArticleVo.setId(String.valueOf(articleVo.getId()));
        return redisArticleVo;
    }
    /**
     * 首页 最新文章
     * @return
     */
    @PostMapping("/articles/new")
    public Result newArticles(){
        int limit = 2;
        List<ArticleVo> articleVoList = articleService.newArticles(limit);
        return Result.ok().data(articleVoList);
    }
    /**
     * 首页 文章归档
     * @return
     */
    @PostMapping("/articles/listArchives")
    public Result listArchives(){
        List<Archives> archivesList = articleService.listArchives();
        return Result.ok().data(archivesList);
    }
    /**
     * 浏览 博客文章 显示文章详情 阅读数量+1
     * @return
     */
    @PostMapping("/articles/view/{id}")
    public Result findArticleById(@PathVariable("id") Long id) {
        ArticleVo articleVo = articleService.findArticleVoById(id);

        return Result.ok().data(articleVo);
    }
    /*
    * 发布博客文章
    * */
    @PostMapping("/articles/publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }
}

